package net.hands.common.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
/**
 * 
 * 说明:Pop3服务
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class Pop3Server implements ReceiveMailSessionProvider {
   /**
    * pop3端口
    */
	protected static final String MAIL_POP3_PORT = "mail.pop3.port";
	/**
	 * pop3主机
	 */
	protected static final String MAIL_POP3_HOST = "mail.pop3.host";
   /**
    * pop3认证
    */
	protected static final String MAIL_POP3_AUTH = "mail.pop3.auth";
   /**
    * pop3协议
    */
	protected static final String PROTOCOL_POP3 = "pop3";
   /**
    * 默认端口
    */
	protected static final int DEFAULT_POP3_PORT = 110;
   /**
    * 端口
    */
	protected final String host;
	/**
	 * 主机
	 */
	protected final int port;
	/**
	 * 认证
	 */
	protected final Authenticator authenticator;
	/**
	 * 会话属性
	 */
	protected final Properties sessionProperties;
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午10:56:20  
   * @param host 端口
    */
	public Pop3Server(final String host) {
		this(host, DEFAULT_POP3_PORT, null);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午10:56:25  
   * @param host 主机
   * @param port 端口
    */
	public Pop3Server(final String host, final int port) {
		this(host, port, null);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午10:56:42  
   * @param host 主机
   * @param authenticator 认证
    */
	public Pop3Server(final String host, final Authenticator authenticator) {
		this(host, DEFAULT_POP3_PORT, authenticator);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午10:56:55  
   * @param host 主机
   * @param port 端口
   * @param username 用户名
   * @param password 密码
    */
	public Pop3Server(final String host, final int port, final String username, final String password) {
		this(host, port, new SimpleAuthenticator(username, password));
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午10:57:06  
   * @param host 主机
   * @param port 端口
   * @param authenticator 认证
    */
	public Pop3Server(final String host, final int port, final Authenticator authenticator) {
		this.host = host;
		this.port = port;
		this.authenticator = authenticator;
		sessionProperties = createSessionProperties();
	}
   /**
   * 说明:创建会话属性
   * @return Properties
   * @update 2012-9-21 上午10:57:12
    */
	protected Properties createSessionProperties() {
		final Properties props = new Properties();
		props.setProperty(MAIL_POP3_HOST, host);
		props.setProperty(MAIL_POP3_PORT, String.valueOf(port));
		if (authenticator != null) {//认证不为空
			props.setProperty(MAIL_POP3_AUTH, "true");
		}
		return props;
	}
   /**
    * 
   (non-Javadoc)  
   * @see net.hands.common.mail.ReceiveMailSessionProvider#createSession()  
   * @update 2012-9-21 上午11:00:09
    */
	@Override
	public ReceiveMailSession createSession() {
		final Session session = Session.getInstance(sessionProperties, authenticator);
		Store store;
		try {
			store = getStore(session);
		} catch (final NoSuchProviderException nspex) {
			throw new MailException("Unable to create POP3 session", nspex);
		}
		return new ReceiveMailSession(session, store);
	}
   /**
   * 说明:获得存储
   * @param session 会议
   * @return Store
   * @throws NoSuchProviderException
   * @update 2012-9-21 上午11:00:17
    */
	protected Store getStore(final Session session) throws NoSuchProviderException {
		return session.getStore(PROTOCOL_POP3);
	}

	// ---------------------------------------------------------------- getters
   /**
   * 说明:获得host头
   * @return String
   * @update 2012-9-21 上午11:00:49
    */
	public String getHost() {
		return host;
	}
   /**
   * 说明:获得认证
   * @return Authenticator
   * @update 2012-9-21 上午11:01:12
    */
	public Authenticator getAuthenticator() {
		return authenticator;
	}
   /**
   * 说明:获得端口
   * @return int
   * @update 2012-9-21 上午11:01:27
    */
	public int getPort() {
		return port;
	}
}